//package wmmd.disc.webmaster;

import java.io.*;
//import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import db.DatabaseConnector;

public class WriteMessageElements extends HttpServlet {
    
    String tName = "";
    
//doPost method included for POST requests
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        doGet(request, response);
    }
    
//Displays the database values for the selected message in a form for editing
//This subroutine is called by EditPost.jsp
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        //The JSP document passes in which db table to use
        tName = request.getParameter("tName");
        String mID = request.getParameter("ID");
        
        //Variables for the database connection
        DatabaseConnector con = new DatabaseConnector(DatabaseConnector.getDatabaseType());
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        try {
            String SQL = "SELECT Name,Subject,Message FROM " + tName + " WHERE ID = ?";
            String[][] rs = con.selectSQL(SQL, 3,new Object[]{mID});

            rs = con.selectWhereSQL(tName, new String[]{"*"}, "ID", mID);
            
            // Assign the results to strings
            String rsName = rs[0][0];
            String rsSubject = rs[0][1];
            String rsMessage = rs[0][2];
            /*String rsName = rs.getString("Name");
            String rsSubject = rs.getString("Subject");
            String rsMessage = rs.getString("Message");*/
            
            // Fill in the form values from the database
            out.println("<table border=\"0\" cellpadding=\"3\" cellspacing=\"3\" width=\"575\">");
            out.println("<tr><td colspan=\"2\" bgcolor=\"cccccc\"><b>Message Information</b></td></tr>");
            
            out.println("<tr><td bgcolor=\"eeeeee\"><b>Message From:</b>&nbsp;</td>");
            out.print("<td><input name=\"DISCName\" type=\"text\" size=\"30\" value=\"");
            out.println(rsName + "\"></td></tr>");
                        
            out.print("<tr><td bgcolor=\"eeeeee\"><b>Subject:</b>&nbsp;</td>");
            out.print("<td><input name=\"DISCSubject\" type=\"text\" size=\"30\" value=\"");
            out.println(rsSubject + "\"></td></tr></table>");
            
            // Add the Message TEXTAREA
            out.println("<p>Edit the <b>Message</b> below:<br />");
            out.println("<textarea name=\"DISCMessage\" cols=\"55\" rows=\"14\">");
            out.println(rsMessage);
            out.println("</textarea></p>");
        }
        finally
        {
            con.CleanUp();
        }
    }
}
